package com.tanhua.manage.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.manage.service.VideoPageServiceImpl;
import com.tanhua.manage.vo.PageResult;
import com.tanhua.manage.vo.VideoParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Slf4j
public class VideoPageController {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private VideoPageServiceImpl videoPageService;

    @GetMapping("manage/videos")
    public ResponseEntity<PageResult> PageVideoList(VideoParam param){


        //获取参数
        //判断page是否为空
        Integer page = param.getPage();            //当前页数
        if (ObjectUtil.isEmpty(page)){
            page = 1;
        }
        //判断pagesize是否为空
        Integer pagesize = param.getPagesize();     //页尺寸
        if (ObjectUtil.isEmpty(pagesize)){
            pagesize= 10;
        }
        String sortProp = param.getSortProp();      //排序字段
        String sortOrder = param.getSortOrder();    //排序顺序
        Long userId = param.getUid();

        try {
            if (page <= 0) {
                page = 1;
            }
            PageResult pageResult = this.videoPageService.queryVideoVoList(userId,page, pagesize,sortProp,sortOrder);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("视频查询失败",e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
